package com.service.Impl;

import com.entity.Student;
import com.mapper.StudentDao;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO
 *
 * @author 95741
 * @version 1.0
 * @date 2020/12/30 19:49
 */
@Service("StudentService")
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;

    @Autowired
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public Student selectByPrimaryKey(Integer id) {
        Student student = studentDao.selectByPrimaryKey(1);
        return student;
    }

    @Override
    public List<Student> findAllStudent() {
        List<Student> all = studentDao.findAll();
        return all;
    }

    @Override
    public List<Student> findStudent(Student student) {
        List<Student> students = studentDao.find(student);
        return students;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insertStudent(Student student) {
        return studentDao.insert(student);
    }

    @Override
    public int delete(String idstr) {
        return studentDao.delete(idstr);
    }

    @Override
    public Student findbyid(Integer id) {
        return studentDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(Student record) {
        int i = studentDao.updateByPrimaryKey(record);
        return i;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void transferMoney(Student newStudent, Student oldStudent, Integer allmoney, Integer newmoney) throws Exception {
        if (allmoney < newmoney || newmoney < 0) {
            throw new Exception();
        }
        studentDao.updateByStudent(newStudent);
        studentDao.updateByStudent(oldStudent);

    }

    @Override
    public String selectNameByid(Integer id) {
        return studentDao.selectNameByid(id);
    }

    @Override
    public Integer selectIdByName(String username) {
        return studentDao.selectIdByName(username);
    }
}
